package benny.ApkposStore.utils;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class Rotate3dView extends FrameLayout {

	private float parentWidth = 200;
	private float parentHeight = 200;
	private Context context;
	private int FRONT_BACK = 0 ;
	private ImageView frontImage;
	private ImageView backImage;
	
	public Rotate3dView(Context context) {
		super(context);
		this.context = context;
	}

	public Rotate3dView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
	}
	
	public float getParentWidth() {
		return parentWidth;
	}

	public void setParentWidth(float parentWidth) {
		this.parentWidth = parentWidth;
	}

	public float getParentHeight() {
		return parentHeight;
	}

	public void setParentHeight(float parentHeight) {
		this.parentHeight = parentHeight;
	}
	
	public void startchange() {
		if(FRONT_BACK == 0){
			FRONT_BACK = 1;
			 applyRotation(1, 0, 90); 
		}else if(FRONT_BACK==1){
			FRONT_BACK = 0;
			 applyRotation(-1,  0, 90); 
		}
	
	}
	public void setRotateImage(int frontImageId,int backImageId){
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int)parentWidth,(int)parentHeight);
		frontImage  = new ImageView(context);
		frontImage.setImageResource(frontImageId);
		addView(frontImage, params);
		backImage  = new ImageView(context);
		backImage.setImageResource(backImageId);
		addView(backImage, params);
		backImage.setVisibility(View.GONE);
		
	}
	
	
	 /**  
     * Setup a new 3D rotation on the container view.  
     *  
     * @param position the item that was clicked to show a picture, or -1 to show the list  
     * @param start the start angle at which the rotation must begin  
     * @param end the end angle of the rotation  
     */ 
    private void applyRotation(int position, float start, float end) {  
        // 计算中心点  
        final float centerX = parentWidth / 2.0f;  
        final float centerY = parentHeight / 2.0f;  
 
        // Create a new 3D rotation with the supplied parameter  
        // The animation listener is used to trigger the next animation  
        final Rotate3dAnimation rotation =  
                new Rotate3dAnimation(start, end, centerX, centerY, 300.0f, true);  
        rotation.setDuration(500);  
        rotation.setFillAfter(true);  
        rotation.setInterpolator(new AccelerateInterpolator());  
        //设置监听  
        rotation.setAnimationListener(new DisplayNextView(position));  
        startAnimation(rotation);  
    }  
 
    
    /**  
     * This class listens for the end of the first half of the animation.  
     * It then posts a new action that effectively swaps the views when the container  
     * is rotated 90 degrees and thus invisible.  
     */ 
    private final class DisplayNextView implements Animation.AnimationListener {  
        private final int mPosition;  
 
        private DisplayNextView(int position) {  
            mPosition = position;  
        }  
 
        public void onAnimationStart(Animation animation) {  
        }  
        //动画结束  
        public void onAnimationEnd(Animation animation) {  
            post(new SwapViews(mPosition));  
        }  
 
        public void onAnimationRepeat(Animation animation) {  
        }  
    }  
 
    /**  
     * This class is responsible for swapping the views and start the second  
     * half of the animation.  
     */ 
    private final class SwapViews implements Runnable {  
        private final int mPosition;  
 
        public SwapViews(int position) {  
            mPosition = position;  
        }  
 
        public void run() {  
        	   final float centerX = parentWidth / 2.0f;  
               final float centerY = parentHeight / 2.0f;  
            Rotate3dAnimation rotation;  
 
            if (mPosition > -1) {  
            	//跳转到第二张图片
                rotation = new Rotate3dAnimation(270, 360, centerX, centerY, 300.0f, false);  
                frontImage.setVisibility(View.GONE);
                backImage.setVisibility(View.VISIBLE);
                backImage.requestFocus();
            } else {  
                //返回前图片 
            	frontImage.setVisibility(View.VISIBLE);
                backImage.setVisibility(View.GONE);
                frontImage.requestFocus();
                rotation = new Rotate3dAnimation(90, 0, centerX, centerY, 300.0f, false);  
            }  
 
            rotation.setDuration(500);  
            rotation.setFillAfter(true);  
            rotation.setInterpolator(new DecelerateInterpolator());  
            //开始动画  
            startAnimation(rotation);  
        }  
    }  
	
}
